package com.example.Project.user;

import com.example.Project.Class.MassOrganizations;
import com.example.Project.user.userHttpMethods.userMethods;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

public class LookMyMassController {
    private static int id;
    void start() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("lookMyMass.fxml"));
        Scene scene = new Scene((Parent)fxmlLoader.load(), 695.0, 306.0);
        Stage stage=new Stage();
        stage.setTitle("社团信息管理");
        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }
    void get(int theId){
        id=theId;
        System.out.println(id);
    }
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private ContextMenu menu;

    /**
     * 退出社团
     * @param actionEvent
     */
    public void btnDel(ActionEvent actionEvent) {
        boolean re=userMethods.userSendDel(id,table.getSelectionModel().getSelectedItem().getId(),2);
        Alert al = new Alert(Alert.AlertType.CONFIRMATION);
        if(re){
            al.setHeaderText("申请已发送，请耐心等待结果！！！");
        }else {
            al.setHeaderText("申请失败，请不要重复申请，若非重复申请失败，请重新申请！！！");
        }
        al.showAndWait();
    }

    @FXML
    private TableView<MassOrganizations> table;
    @FXML
    private TableColumn<MassOrganizations, Integer> tableId;
    @FXML
    private TableColumn<MassOrganizations, String> tableName;
    @FXML
    private TableColumn<MassOrganizations, Date> tableBirthday;
    @FXML
    private TableColumn<MassOrganizations, String> tableType;
    @FXML
    private TableColumn<MassOrganizations, String> tablePhone;
    userMethods userMethods=new userMethods();
    @FXML
    public void initialize() throws ClassNotFoundException, IOException {
        table.getItems().clear();
        ObservableList<MassOrganizations> massOrganizationsList= userMethods.getUserMass(id);
        System.out.println(massOrganizationsList);
        Platform.runLater(() -> updateTableView(massOrganizationsList));
    }
    private void updateTableView(List<MassOrganizations> MassOrganizations) {
        this.table.getItems().addAll(MassOrganizations);
        this.tableId.setCellValueFactory(new PropertyValueFactory<>("Id"));
        this.tableName.setCellValueFactory(new PropertyValueFactory<>("Name"));
        this.tableBirthday.setCellValueFactory(new PropertyValueFactory<>("Birthday"));
        this.tableType.setCellValueFactory(new PropertyValueFactory<>("Type"));
        this.tablePhone.setCellValueFactory(new PropertyValueFactory<>("Phone"));
    }
}
